<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 前台会员ajax类
 * @package  ajax
 * @subpackage  shop
 * @author   allen.qiang
 */

quanli_require('module/base/customer/quanliCust.class.php');
quanli_require('module/base/activity/activityConfig.class.php');//活动配置文件
quanli_include('module/base/promotion/quanliCoupon.class.php');//加载优惠券类

class ajax_customer {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //注册会员
                    $this->register();
                    break;

                case 2: //会员登录
                    $this->login();
                    break;

                case 3: //检查会员名
                    $this->checkCustomer();
                    break;

                case 4: //找回密码
                    $this->getPwd();
                    break;

                case 5: //修改资料
                    $this->editCustomer();
                    break;

                case 6:
                    $this->alertPwd();
                    break;

                case 7:
                    $this->quickLogin();
                    break;

                case 8: //检测会员状况
                    $this->getCheckCustomer();
                    break;

                case 9: //修改会员手机号码
                    $this->editMobilePhone();
                    break;
                case 10:
                    $this->wap_register();
                    break;
                case 11:
                    $this->checkSms();
                    break;
                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {
            returnJsonData('参数错误');
        }

    }
    

    /**
     * 检查会员名
     */
    private function checkCustomer() {

        if (!isset($_REQUEST['regName']) || !is_mobile($_REQUEST['regName'])) {
            returnJsonData('请输入手机号码');
        }

        //实例会员类
        $quanliCust = new quanliCust();

        //查询会员信息
        $arg_array = array(
            'custID' => $_REQUEST['regName']
        );
        $cust_array = $quanliCust->searchCustomerDetail( $arg_array );

        if ($cust_array) {

            returnJsonData('该会员名已被使用', 404, array( 'type' => $cust_array['custType'] ));

        }

        returnJsonData('ok', 200);

    }

    /**
     * 检测会员
     */
    private function getCheckCustomer(){

        if (!isset($_REQUEST['regName']) || (!is_mobile($_REQUEST['regName'])) && !is_email($_REQUEST['regName'])) {
            returnJsonData('请输入手机号码');
        }

        if (!isset($_REQUEST['act']) || empty($_REQUEST['act'])) {
            returnJsonData('参数错误');
        }

        //实例会员类
        $quanliCust = new quanliCust();

        //查询会员信息
        $arg_array = array(
            'custID' => $_REQUEST['regName']
        );
        $cust_array = $quanliCust->searchCustomerDetail( $arg_array );
        if(!$cust_array && $_REQUEST['act'] == 'login'){
            returnJsonData('此号码为新用户，需注册', 200);
        }

        if($cust_array && $_REQUEST['act'] == 'register'){
            returnJsonData('此号码已注册', 200);
        }

    }



    /**
     * 添加会员信息
     */
    private function register() {

        global $G_SHOP;

        if (!isset($_REQUEST['custSource']) || !in_array($_REQUEST['custSource'], array(1, 2, 4))) {
            $_REQUEST['custSource'] = 1;
        }
        if (!isset($_REQUEST['custSourceExt']) || empty($_REQUEST['custSourceExt'])) {
            $_REQUEST['custSourceExt'] = null;
        }

        //检查短信验证码
        $sms_ysm = $G_SHOP->sessionCache_get('sms_yzm_' . $_REQUEST['regName']);

        if (is_null($sms_ysm)) {
            returnJsonData('短信验证码已失效', 400);
        } elseif ($sms_ysm != md5($_REQUEST['smsyzm'])) {
            returnJsonData('短信验证码有误');
        }

        $G_SHOP->sessionCache_remove( 'sms_yzm_' . $_REQUEST['regName'] );
        $G_SHOP->sessionCache_remove( 'sms_yzm__time_' . $_REQUEST['regName'] );

        /**
         * =============================
         * 前台注册会员信息
         * =============================
         */

        //实例会员类
        $quanliCust = new quanliCust();

        //前台注册会员信息
        $args_array = array(
            'custID' => $_REQUEST['regName'],
            'pwd' => $_REQUEST['regPwd'],
            'pwd2' => $_REQUEST['regPwd2'],
            'custSource' => $_REQUEST['custSource'],
            'custSourceExt' => $_REQUEST['custSourceExt'],
            'custType' => 1,
        );
        $result_array = $quanliCust->register( $args_array );
        if ($result_array['code'] != 200) {
            returnJsonData($result_array['message'], $result_array['code']);
        }
        /**
         * =============================
         * 前台注册会员信息
         * =============================
         */


        /**
         * =============================
         * 更新会员登录信息
         * =============================
         */
        $quanliCust->shopCheckin( $result_array['data'] );
        /**
         * =============================
         * 更新会员登录信息
         * =============================
         */


        /**
         * ==========================
         * 渠道会员
         * ==========================
         */
        /*此功能迁移到class ebsigCust 内部执行 registerCustChannel()
        $tracker_code = $G_SHOP->get_cookie_info('__etrack_tracker_code');
        $key1 = $G_SHOP->get_cookie_info('__etrack_key1');
        $key2 = $G_SHOP->get_cookie_info('__etrack_key2');
        $key3 = $G_SHOP->get_cookie_info('__etrack_key3');
        $referrer = $G_SHOP->get_cookie_info('__etrack_referrer');
        $referrer_time = $G_SHOP->get_cookie_info('__etrack_referrer_time');
        $tracker = $G_SHOP->get_cookie_info('__etrack');
        $tracker_array = explode('|', $tracker);
        $etrack_switch = $G_SHOP->get_cookie_info('__etrack_switch');

        $cust_channel = new entity('cust_channel');
        $cust_channel->pcustID = $result_array['data']['pcustID'];
        if (!$G_SHOP->DBCA->fetch($cust_channel)) {
            $cust_channel->creator = 'system';
            $cust_channel->source_url = $referrer;
            $cust_channel->source_time = $referrer_time;
            $cust_channel->key1 = $key1;
            $cust_channel->key2 = $key2;
            $cust_channel->key3 = $key3;
            $cust_channel->tracker_code = $tracker_code;
            $cust_channel->session_id = $tracker_array[0];
            $cust_channel->switch_id = $etrack_switch;
            $cust_channel->ip = get_ip();
            $cust_channel->source_channel = $_REQUEST['custSource'] == 1 ? 'web' : 'wap';
            $G_SHOP->DBCA->save( $cust_channel );
        }
        */

        //rand(100,999)
       // mediav 流量代码 add by allen.qiang 20150416 start
        if( $_REQUEST['custSource'] == 2)
        {
            //加载类文件
            quanli_include('module/base/common/quanliHttp.class.php');

            //实例http类
            $quanliHttp = new quanliHttp();
            $ret = $quanliHttp->get('http://mvp.mediav.com/t?type=3&db=none&jzqv=3&qzjpt=registered&jzqs=m-26116-0&jzqt=tran&jzqo='.rand(100,999).'&jzqot=0&jzqo4=0');

        }
        // mediav 流量代码 add by allen.qiang 20150416 end
        /**
         * ==========================
         * 渠道会员
         * ==========================
         */

        if ($G_SHOP->sessionCache_get('WECHAT_CUSTOMER_UUID')) {

            //实例微电汇类
            $wdh_obj = $G_SHOP->eai('wdh');

            $args_data = array(
                'wechat_uid' => $G_SHOP->sessionCache_get('WECHAT_CUSTOMER_UUID'),
                'pcustID' => $result_array['data']['pcustID'],
                'custID' => $result_array['data']['custID'],
                'rankID' => $result_array['data']['rankID'],
                'rankName' => $result_array['data']['rankName'],
                'sumAmt' => 0,
                'custSource' => $result_array['data']['custSource'],
                'register_time' => $result_array['data']['register_time'],
                'mobile' => $result_array['data']['mobile'],
                'email' => null,
                'custName' => null
            );
            $return_data = $wdh_obj->bindWechatCustomer($args_data);
            if ($return_data['code'] == 200) {
                $G_SHOP->sessionCache_remove('WECHAT_CUSTOMER_UUID');
            }

        }


        returnJsonData('ok', 200, $result_array['data']['custID']);

    }

    /**
     * 手机注册添加会员信息 发放优惠劵    add by guojun.song   2015-04-16
     */
    private function wap_register() {

        global $G_SHOP;

        //实例化配置类
        $activity_config = new activityConfig('CouponAA');

        $config_array =  $activity_config->config;


        if(date('Y-m-d H:i:s') < $config_array['start_time'] || date('Y-m-d H:i:s') > $config_array['end_time']){
            returnJsonData('活动不在时间范围内', '501');
        }


        if (!isset($_REQUEST['custSource']) || !in_array($_REQUEST['custSource'], array(1, 2, 4))) {
            $_REQUEST['custSource'] = 1;
        }

        //检查短信验证码
        $sms_ysm = $G_SHOP->sessionCache_get('sms_yzm_' . $_REQUEST['regName']);

        if (is_null($sms_ysm)) {
            returnJsonData('短信验证码已失效', 400);
        } elseif ($sms_ysm != md5($_REQUEST['code'])) {
            returnJsonData('短信验证码有误');
        }

        //实例会员类
        $quanliCust = new quanliCust();

        //前台注册会员信息
        $args_array = array(
            'custID' => $_REQUEST['regName'],
            'pwd' => $_REQUEST['password'],
            'pwd2' => $_REQUEST['comfirm_password'],
            'custSource' => $_REQUEST['custSource'],
            'custType' => 1
        );
        $result_array = $quanliCust->register( $args_array );
        if ($result_array['code'] != 200) {
            returnJsonData($result_array['message'], $result_array['code']);
        }
        /**
         * =============================
         * 前台注册会员信息
         * =============================
         */


        /**
         * =============================
         * 更新会员登录信息
         * =============================
         */
        $quanliCust->shopCheckin( $result_array['data'] );
        /**
         * =============================
         * 更新会员登录信息
         * =============================
         */


        if ($G_SHOP->sessionCache_get('WECHAT_CUSTOMER_UUID')) {

            //实例微电汇类
            $wdh_obj = $G_SHOP->eai('wdh');

            $args_data = array(
                'wechat_uid' => $G_SHOP->sessionCache_get('WECHAT_CUSTOMER_UUID'),
                'pcustID' => $result_array['data']['pcustID'],
                'custID' => $result_array['data']['custID'],
                'rankID' => $result_array['data']['rankID'],
                'rankName' => $result_array['data']['rankName'],
                'sumAmt' => 0,
                'custSource' => $result_array['data']['custSource'],
                'register_time' => $result_array['data']['register_time'],
                'mobile' => $result_array['data']['mobile'],
                'email' => null,
                'custName' => null
            );
            $return_data = $wdh_obj->bindWechatCustomer($args_data);
            if ($return_data['code'] == 200) {
                $G_SHOP->sessionCache_remove('WECHAT_CUSTOMER_UUID');
            }

        }



        //实例优惠券类
        $ebsigCoupon = new quanliCoupon();

        $couponID = $config_array['coupon_id'];

        //发送优惠券
        $ebsigCoupon->createCouponCode($couponID,array($_REQUEST['regName']),false);

        returnJsonData('ok', 200, $result_array['data']['custID']);

    }

    /**
     * 查询会员信息
     */
    private function login() {

        global $G_SHOP;

        //检查参数
        if (!isset($_REQUEST['loginName'], $_REQUEST['loginPwd']) || empty($_REQUEST['loginName']) || empty($_REQUEST['loginPwd'])) {
            returnJsonData('缺少参数', 1);
        }

        //实例会员类
        $quanliCust = new quanliCust();

        //查询会员信息
        $arg_array = array(
            'custID' => $_REQUEST['loginName']
        );
        $customer_array = $quanliCust->searchCustomerDetail( $arg_array );
        if (!$customer_array) {
            returnJsonData('此号码为新用户，需注册', 1);
        }

        //检查密码
        if ($customer_array['pwd'] != md5($_REQUEST['loginPwd'])) {
            returnJsonData('密码错误', 2);
        }
        if ($customer_array['useFlg'] == 0) {
            returnJsonData('会员已被禁用', 1);
        }

        //更新会员登录信息
        $quanliCust->shopCheckin( $customer_array );

        /**
         * =============================
         * 检查会员促销活动
         * =============================
         */

        //加载会员促销类
        quanli_include('module/base/promotion/promotionCustomer.class.php');


        //实例会员促销类
        $promotionHandler = new promotionCustomer();


        //查询会员促销活动并发放
        $promotionHandler->grantCustPromotion( $customer_array['pcustID'], $customer_array['custID'], 2 );


        /**
         * =============================
         * 检查会员促销活动
         * =============================
         */

        /**
         * 检查是否存在微信粉丝uuid
         * 存在则把网站会员和微信粉丝绑定
         */
        if ($G_SHOP->sessionCache_get('WECHAT_CUSTOMER_UUID')) {

            //实例微电汇类
            $wdh_obj = $G_SHOP->eai('wdh');

            $args_data = array(
                'wechat_uid' => $G_SHOP->sessionCache_get('WECHAT_CUSTOMER_UUID'),
                'pcustID' => $customer_array['pcustID'],
                'custID' => $customer_array['custID'],
                'custName' => $customer_array['custname'],
                'rankID' => $customer_array['rankID'],
                'rankName' => $customer_array['rankName'],
                'sumAmt' => $customer_array['sumAmt'],
                'custSource' => $customer_array['custSource'],
                'register_time' => $customer_array['createTime'],
                'mobile' => $customer_array['mobile'],
                'email' => $customer_array['email'],
            );
            $return_data = $wdh_obj->bindWechatCustomer($args_data);
            if ($return_data['code'] == 200) {
                $G_SHOP->sessionCache_remove('WECHAT_CUSTOMER_UUID');
            }

        }

        if (isset($_REQUEST['quick'])) {
            returnJsonData('ok', 200, array( $customer_array['custID'], $customer_array['rankID'] ));
        } else {
            returnJsonData('ok', 200, $customer_array['custID']);
        }

    }

    /**
     * 找回密码
     */
    private function getPwd() {

        global $G_SHOP;

        //检查短信验证码
        $sms_ysm = $G_SHOP->sessionCache_get('sms_yzm_' . $_REQUEST['mobile']);
        if (is_null($sms_ysm)) {
            returnJsonData('短信验证码已失效', 400);
        } elseif ($sms_ysm != md5($_REQUEST['smsyzm'])) {
            returnJsonData('短信验证码有误');
        }
        $G_SHOP->sessionCache_remove( 'sms_yzm_' . $_REQUEST['mobile'] );
        $G_SHOP->sessionCache_remove( 'sms_yzm_time_' . $_REQUEST['mobile'] );

        //实例会员类
        $quanliCust = new quanliCust();

        //重置会员密码
        $result_array = $quanliCust->resetCustPwd( $_REQUEST );

        if ($result_array['code'] != 200) {
            returnJsonData($result_array['message'], $result_array['code']);
        }

        //更新会员登录信息
        $quanliCust->shopCheckin( $result_array['data'] );

        returnJsonData('ok', 200, $result_array['data']['custID']);

    }

    /**
     * 修改资料
     */
    private function editCustomer() {

        global $G_SHOP;

        if (!$G_SHOP->currentPCustID) {
            returnJsonData('会员未登录');
        }

        if ( !isset($_REQUEST['mobile'], $_REQUEST['nickName'], $_REQUEST['email'], $_REQUEST['sexy'], $_REQUEST['year'], $_REQUEST['month'], $_REQUEST['day'] )) {
            returnJsonData('参数错误');
        }

        if (!is_mobile($_REQUEST['mobile'])) {
            returnJsonData('请输入正确的手机号码');
        }

        if (empty($_REQUEST['nickName']) || strlen($_REQUEST['nickName']) > 20) {
            returnJsonData('昵称最多填写10个汉字或20个英文字母与数字');
        }

        if (!empty($_REQUEST['email']) && !is_email($_REQUEST['email'])) {
            returnJsonData('请输入正确的邮箱');
        }

        if (!in_array($_REQUEST['sexy'], array(1, 2, 3))) {
            $_REQUEST['sexy'] = 0;
        }

        //根据手机号码查询会员信息
        $cust_obj = new entity('cust_master');
        $cust_obj->pcustID = $G_SHOP->currentPCustID;
        if (!$G_SHOP->DBCA->fetch($cust_obj)) {
            returnJsonData('会员信息没有找到', 404);
        }
        $cust_obj->mobile = encryptD($_REQUEST['mobile']);
        $cust_obj->email = encryptD($_REQUEST['email']);
        $cust_obj->nickName = $_REQUEST['nickName'];
        $cust_obj->sexy = $_REQUEST['sexy'];
        if (empty($cust_obj->birthday) || $cust_obj->birthday == '0000-00-00') {
            $cust_obj->birthday = $_REQUEST['year'] . '-' . $_REQUEST['month'] . '-' . $_REQUEST['day'];
        }
        $G_SHOP->DBCA->save($cust_obj);

        /**
         * =============================
         * 检查会员促销活动
         * =============================
         */

        //加载会员促销类
        quanli_include('module/base/promotion/promotionCustomer.class.php');


        //实例会员促销类
        $promotionHandler = new promotionCustomer();


        //查询会员促销活动并发放
        $promotionHandler->grantCustPromotion( $cust_obj->pcustID, $cust_obj->custID, 6 );


        /**
         * =============================
         * 检查会员促销活动
         * =============================
         */

        returnJsonData('ok', 200);

    }

    /**
     * 修改密码
     *
     */
    private function alertPwd() {

        global $G_SHOP;

        if (!$G_SHOP->currentPCustID) {
            returnJsonData('会员未登录');
        }

        //检查短信验证码
        $sms_ysm = $G_SHOP->sessionCache_get('sms_yzm_' . $_REQUEST['mobile']);
        if (is_null($sms_ysm)) {
            returnJsonData('短信验证码已失效', 400);
        } elseif ($sms_ysm != md5($_REQUEST['smsyzm'])) {
            returnJsonData('短信验证码有误');
        }
        $G_SHOP->sessionCache_remove( 'sms_yzm_' . $_REQUEST['mobile'] );
        $G_SHOP->sessionCache_remove( 'sms_yzm_time_' . $_REQUEST['mobile'] );


        //实例会员类
        $quanliCust = new quanliCust();

        $_REQUEST['pcustID'] = $G_SHOP->currentPCustID;
        $result_array = $quanliCust->editCustPwd( $_REQUEST );

        echo json_encode( $result_array );

    }

    private function quickLogin() {

        //检查参数
        if (!isset($_REQUEST['loginName'], $_REQUEST['loginPwd']) || empty($_REQUEST['loginName'])) {
            returnJsonData('缺少参数');
        }

        $guest = 0; //前台登录会员类型 1、无密码登录的用户 0、会员 2、有密码但没使用

        //实例会员类
        $quanliCust = new quanliCust();

        //查询会员信息
        $arg_array = array(
            'custID' => $_REQUEST['loginName']
        );
        $customer_array = $quanliCust->searchCustomerDetail( $arg_array );
        if ($customer_array) {

            //检查密码
            if (!empty($_REQUEST['loginPwd'])) {
                if ($customer_array['pwd'] != md5($_REQUEST['loginPwd'])) {
                    returnJsonData('密码错误', 2);
                }
            } else {
                if ( $customer_array['custType'] == 1 ) {
                    $guest = 2;
                } else {
                    $guest = 1;
                }
            }
            if ($customer_array['useFlg'] == 0) {
                returnJsonData('会员已被禁用', 404);
            }


        } else {

            $guest = 1;

            $pwd = get_random_string(6);

            //前台注册会员信息
            $args_array = array(
                'custID' => $_REQUEST['loginName'],
                'pwd' => $pwd,
                'pwd2' => $pwd,
                'custSource' => 1,
                'custType' => 2
            );
            $result_array = $quanliCust->register( $args_array );
            if ($result_array['code'] != 200) {
                returnJsonData($result_array['message'], $result_array['code']);
            }

            $customer_array = $result_array['data'];

        }



        //更新会员登录信息
        $quanliCust->shopCheckin( $customer_array, $guest );


        if ($guest == 0) {

            /**
             * =============================
             * 检查会员促销活动
             * =============================
             */

            //加载会员促销类
            quanli_include('module/base/promotion/promotionCustomer.class.php');


            //实例会员促销类
            $promotionHandler = new promotionCustomer();


            //查询会员促销活动并发放
            $promotionHandler->grantCustPromotion( $customer_array['pcustID'], $customer_array['custID'], 2 );


            /**
             * =============================
             * 检查会员促销活动
             * =============================
             */

        }

        returnJsonData('ok', 200, array( 'id' => $customer_array['custID'], 'guest' => $guest ));

    }


    /*
     *
     * 修改会员手机号码
     */
    public function editMobilePhone(){
        global $G_SHOP;

        if (!$G_SHOP->currentPCustID) {
            returnJsonData('会员未登录');
        }
        if( !isset($_REQUEST['pwd'],$_REQUEST['mobile'])){
            returnJsonData('缺少参数', 400);
        }

        //实例会员类
        $quanliCust = new quanliCust();

        //获取客户号
        $pcustID = $G_SHOP->currentPCustID;

        //查询会员信息
        $args_array = array(
            'pcustID' => $pcustID
        );
        $custInfo = $quanliCust->searchCustomerDetail( $args_array );
        $oldPwd = $custInfo['pwd'];
        //检查密码
        if( empty($_REQUEST['pwd']) ){
            returnJsonData('密码不能为空', 400);
        }else if( md5($_REQUEST['pwd']) !=  $oldPwd ){
            returnJsonData('登录密码错误，请输入正确的登录密码！', 400);
        }

        //检查手机号
        if( $_REQUEST['mobile'] == $custInfo['mobile']){
            returnJsonData('新手机号不能与旧手机号相同！', 400);
        }

        //检查短信验证码
        $sms_ysm = $G_SHOP->sessionCache_get('sms_yzm_' . $_REQUEST['mobile']);
        if (is_null($sms_ysm)) {
            returnJsonData('短信验证码已失效', 400);
        } elseif ($sms_ysm != md5($_REQUEST['smsyzm'])) {
            returnJsonData('短信验证码有误');
        }
        $G_SHOP->sessionCache_remove( 'sms_yzm_' . $_REQUEST['mobile'] );
        $G_SHOP->sessionCache_remove( 'sms_yzm_time_' . $_REQUEST['mobile'] );


        $_REQUEST['pcustID'] = $G_SHOP->currentPCustID;
        $result_array = $quanliCust->editCustMobile( $_REQUEST );

        echo json_encode( $result_array );
    }

    /**
     * 验证手机验证码
     */
    private function checkSms() {

        global $G_SHOP;

        if (!isset($_REQUEST['custSource']) || !in_array($_REQUEST['custSource'], array(1, 2, 4))) {
            $_REQUEST['custSource'] = 1;
        }

        //检查短信验证码
        $sms_ysm = $G_SHOP->sessionCache_get('sms_yzm_' . $_REQUEST['regName']);

        if (is_null($sms_ysm)) {
            returnJsonData('短信验证码已失效', 400);
        } elseif ($sms_ysm != md5($_REQUEST['smsyzm'])) {
            returnJsonData('短信验证码有误');
        } else {
            returnJsonData('ok', 200);
        }
    }
}